package net.perretxiko.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.perretxiko.form.Batu;
import net.perretxiko.form.Erabiltzaile;
import net.perretxiko.form.JsonResponse;
import net.perretxiko.service.ErabiltzaileService;
import net.perretxiko.service.PerretxikoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

@Controller
public class WebController {
	
	@Autowired
	private ErabiltzaileService erabiltzaileService;
	@Autowired
	private PerretxikoService perretxikoService;
	
	@RequestMapping("/kontaktatu")
	public ModelAndView kontaktatu() {
		return new ModelAndView("kontaktatu","bidalita", false);
	}
	
//	@RequestMapping("/kontak")
//	public ModelAndView kontak() {
//		return new ModelAndView("kontaktatu","bidalita", true);
//	}
	@RequestMapping(value = "/kontak", method = RequestMethod.POST)
	public ModelAndView kontaktatuBidali() {
		return new ModelAndView("kontaktatu","bidalita", true);
	}
	
	
	@RequestMapping("/mapak")
	public ModelAndView mapak(){
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//		System.out.println(user.getUsername());
		List<Batu> zesta = perretxikoService.GetZesta(1);
		ArrayList<String> jsonOutput = new ArrayList<String>();
		for(int i=0;i<zesta.size();i++){
			Gson gson = new Gson();
			jsonOutput.add(gson.toJson(zesta.get(i)));
		}
		
//		Gson gson = new Gson();
//		jsonOutput.add(gson.toJson(era));
		return new ModelAndView("mapak", "jsonOutput", jsonOutput);
	}
	
	@RequestMapping("/enziklopedia")
	public ModelAndView enziklopedia() {
		
		return new ModelAndView("enziklopedia");
	}
	
	@RequestMapping("/pertsonala")
	public ModelAndView pertsonala(Map<String, Object> map) {
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		user.getUsername();
		Erabiltzaile era = erabiltzaileService.getErabiltzaile(user.getUsername());		
		map.put("erabiltzaile", era);		
		return new ModelAndView("pertsonala", "era", era);
	}
	
	@RequestMapping("/aldatu")
	public ModelAndView aldatu(Map<String, Object> map) {
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		user.getUsername();
		Erabiltzaile era = erabiltzaileService.getErabiltzaile(user.getUsername());		
		map.put("erabiltzaile", era);		
		return new ModelAndView("aldatu", "command", era);
	}
	
	@RequestMapping(value = "/aldatuerabiltzailea", method = RequestMethod.POST)
	public ModelAndView addErabiltzaile(@ModelAttribute("erabiltzaile")
	Erabiltzaile erabiltzaile, BindingResult result, Map<String, Object> map) {
		erabiltzaileService.aldatuErabiltzaile(erabiltzaile);
		Erabiltzaile era = erabiltzaileService.getErabiltzaile(erabiltzaile.getErabiltzailea());		
		map.put("erabiltzaile", era);		
		return new ModelAndView("pertsonala", "era", era);
	}

}
